﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace TARADesktopDoctor
{
    /// <summary>
    /// Interaction logic for PatientItem.xaml
    /// </summary>
    public partial class PatientItem : UserControl
    {
        tara_wcf.ItaraServiceClient svc = new tara_wcf.ItaraServiceClient();
        tara_wcf.patient patientDetail = new tara_wcf.patient();

        string latestGamePlayDate;
        string latestAssessmentDate;
        string adminDate;
        public static List<KeyValuePair<int, int>> jointList = new List<KeyValuePair<int, int>>();
        public PatientItem(tara_wcf.patient p)
        {
            this.InitializeComponent();
            patientDetail = p;
            //Populate the item with patient's details
            lblFirstName.Text = p.FirstName.ToString();
            lblLastName.Text = p.LastName.ToString();
            lblAddress.Text = p.Address1.ToString();
            lblBirthDate.Text = p.DateOfBirth.ToString("MMMM dd, yyyy");
            lblGender.Text = p.Gender.ToString();
            lblHomeNumber.Text = p.ContactNumHome.ToString();
            lblIDNumber.Text = p.Nric.ToString();
            lblMobileNumber.Text = p.ContactNumMob.ToString();
            latestGamePlayDate = svc.retrieveLatestGamePlayDate(p.Uid).ToString("MMMM dd, yyyy");
            latestAssessmentDate = svc.retrieveLatestAssessmentDate(p.Uid).ToString("MMMM dd, yyyy");
            adminDate = p.AdminDate.ToString("MMMM dd, yyyy");
            if (p.ImageByte != null)
            {
                imgProfilePicture.Source = LoadImage(p.ImageByte);
            }
        }

        private void ResetSelectionColors()
        {
            if (this.Parent != null)
            {
                if (this.Parent is StackPanel)
                {
                    StackPanel treatmentPanel = (StackPanel)this.Parent;
                    foreach (UIElement element in treatmentPanel.Children)
                    {
                        if (element is PatientItem)
                        {
                            PatientItem pi = (PatientItem)element;
                            foreach (UIElement piElement in pi.InfoPanel.Children)
                            {
                                if (piElement is StackPanel)
                                {
                                    StackPanel sp = (StackPanel)piElement;
                                    foreach (UIElement spElement in sp.Children)
                                    {
                                        if (spElement is TextBlock)
                                        {
                                            TextBlock tb = (TextBlock)spElement;
                                            tb.Foreground = Brushes.Black;
                                        }
                                    }
                                }
                            }
                            pi.Background = null;
                        }
                    }
                }
            }
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MainWindow rootWindow = Application.Current.MainWindow as MainWindow;
            MainPage page = new MainPage(patientDetail);
            rootWindow.frame1.Navigate(page);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            MainWindow rootWindow = Application.Current.MainWindow as MainWindow;
            rootWindow.frame1.Navigate(new EditPatient(patientDetail));
        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResetSelectionColors();
            foreach (UIElement piElement in this.InfoPanel.Children)
            {
                if (piElement is StackPanel)
                {
                    StackPanel sp = (StackPanel)piElement;
                    foreach (UIElement spElement in sp.Children)
                    {
                        if (spElement is TextBlock)
                        {
                            TextBlock tb = (TextBlock)spElement;
                            tb.Foreground = Brushes.White;
                        }
                    }
                }
            }

            this.Background = new SolidColorBrush(Color.FromArgb(255, 47, 113, 204));
            MainWindow main = Application.Current.MainWindow as MainWindow;
            try
            {
                PatientSelectionPage selectionPage = (PatientSelectionPage)main.frame1.Content;
                selectionPage.patientsummary.lblAdmissionDate.Text = adminDate;
                selectionPage.patientsummary.lblAssessmentDate.Text = latestAssessmentDate;
                selectionPage.patientsummary.lblGameplayDate.Text = latestGamePlayDate;
                #region forloopjointList
                #region reset joint background color
                BrushConverter bc = new BrushConverter();
                Brush brush;
                brush = (Brush)bc.ConvertFrom("#FF00A703");
                selectionPage.patientsummary.btnNeck.Background = brush;
                selectionPage.patientsummary.btnLeftShoulder.Background = brush;
                selectionPage.patientsummary.btnRightShoulder.Background = brush;
                selectionPage.patientsummary.btnLeftElbow.Background = brush;
                selectionPage.patientsummary.btnRightElbow.Background = brush;
                selectionPage.patientsummary.btnLeftWrist.Background = brush;
                selectionPage.patientsummary.btnRightWrist.Background = brush;
                selectionPage.patientsummary.btnLeftHip.Background = brush;
                selectionPage.patientsummary.btnRightHip.Background = brush;
                selectionPage.patientsummary.btnLeftKnee.Background = brush;
                selectionPage.patientsummary.btnRightKnee.Background = brush;
                selectionPage.patientsummary.btnLeftAnkle.Background = brush;
                selectionPage.patientsummary.btnRightAnkle.Background = brush;
                #endregion
                #region change impairment joint to red
                foreach (KeyValuePair<int, int> temp in patientDetail.JointSeverity)
                {
                    switch (temp.Key)
                    {
                        case 1:
                            selectionPage.patientsummary.btnNeck.Background = Brushes.Red;
                            break;
                        case 2:
                            selectionPage.patientsummary.btnLeftShoulder.Background = Brushes.Red;
                            break;
                        case 3:
                            selectionPage.patientsummary.btnRightShoulder.Background = Brushes.Red;
                            break;
                        case 4:
                            selectionPage.patientsummary.btnLeftElbow.Background = Brushes.Red;
                            break;
                        case 5:
                            selectionPage.patientsummary.btnRightElbow.Background = Brushes.Red;
                            break;
                        case 6:
                            selectionPage.patientsummary.btnLeftWrist.Background = Brushes.Red;
                            break;
                        case 7:
                            selectionPage.patientsummary.btnRightWrist.Background = Brushes.Red;
                            break;
                        case 9:
                            selectionPage.patientsummary.btnLeftHip.Background = Brushes.Red;
                            break;
                        case 10:
                            selectionPage.patientsummary.btnRightHip.Background = Brushes.Red;
                            break;
                        case 11:
                            selectionPage.patientsummary.btnLeftKnee.Background = Brushes.Red;
                            break;
                        case 12:
                            selectionPage.patientsummary.btnRightKnee.Background = Brushes.Red;
                            break;
                        case 13:
                            selectionPage.patientsummary.btnLeftAnkle.Background = Brushes.Red;
                            break;
                        case 14:
                            selectionPage.patientsummary.btnRightAnkle.Background = Brushes.Red;
                            break;
                    }
                }
                #endregion
                #endregion
            }
            catch (Exception ex)
            {

            }
        }

        BitmapSource LoadImage(Byte[] imageData)
        {
            using (MemoryStream ms = new MemoryStream(imageData))
            {
                var decoder = BitmapDecoder.Create(ms,
                    BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                return decoder.Frames[0];
            }
        }
    }
}